package com.linln.admin.home;

import com.linln.common.vo.JsonResult;
import com.linln.common.vo.PageResult;
import com.linln.modules.homerotation.domain.HomeRotation;
import com.linln.modules.homerotation.service.HomeRotationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.admin.home
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.admin.home
 * @Description: TODO
 * @date Date : 2021年01月11日 16:41
 */
@RequestMapping("activityCarousel")
@Controller
public class ActivityCarouselController {
    @Autowired
    private HomeRotationService homeRotationService;

    @GetMapping("index")
    public String index() {
        return "/home/activityCarousel/index";
    }

    @PostMapping
    @ResponseBody
    public JsonResult addHoneRotation(@RequestParam("file") MultipartFile file, HomeRotation homeRotation) {
        List<HomeRotation> honeRotationList = homeRotationService.getHoneRotation(1);
        if (honeRotationList.size() >= 5) {
            return JsonResult.error("数量不能大于5张");
        }
        homeRotation.setHrType(1);
        return homeRotationService.addHoneRotation(file, homeRotation) ? JsonResult.ok() : JsonResult.error();
    }

    @PutMapping
    @ResponseBody
    public JsonResult updateHoneRotation(@RequestParam("file") MultipartFile file, HomeRotation homeRotation) {
        homeRotation.setHrType(1);
        return homeRotationService.updateHoneRotation(file, homeRotation) ? JsonResult.ok() : JsonResult.error();
    }

    @GetMapping
    @ResponseBody
    public PageResult getHoneRotation() {
        return new PageResult(homeRotationService.getHoneRotation(1));
    }
}
